import axios from 'axios'
import router from '@/router/index'
import qs from 'qs';
import {Message} from "element-ui";
import store from "@/store";

let configList = {
    support : process.env.VUE_APP_BASE_API,
}


let configMap = {}
for(let key in configList){
    const service = axios.create({
        baseURL: configList[key],
        timeout: 160000, // 过期时间
        withCredentials: false
    })
    service.interceptors.request.use(
        config => {
            // 发送请求之前做的
            if (store.state.login.tokenInfo) {
                config.headers[store.state.login.tokenInfo.tokenName] = store.state.login.tokenInfo.tokenValue
            }
            return config
        },
        error => {
            return Promise.reject(error)
        }
    )

    service.interceptors.response.use(function (response) {
            // 对响应数据做点什么
            if (response.data.code === 0) {
                return response.data.result;
            } else if (response.data.code === 10) {
                Message.error(response.data.msg + '请重新登录');
                sessionStorage.removeItem('tokenInfo')
                sessionStorage.removeItem('userInfo')
                sessionStorage.removeItem('userRouteList')
                router.replace('/login')
                return false;
            } else if (response.data.code === -1 || response.data.code === 201) {
                if (router.history.current.path === '/othersWork') {
                } else {
                    Message.error('请求失败:' + response.data.msg);
                }
                return Promise.reject(response.data)
            } else {
                return response
            }
        },
        function (error) {
            if (error && error.response) {
                switch (error.response.status) {
                    case 300:
                        Message.warning('请求错误(300)');
                        break;
                    case 400:
                        Message.warning('请求错误(400)');
                        break;
                    case 401:
                        Message.warning('未授权，请重新登录(401)')
                        break;
                    case 403:
                        Message.warning('拒绝访问(403)')
                        break;
                    case 404:
                        Message.warning('请求出错(404)');
                        break;
                    case 408:
                        Message.warning('请求超时(408)')
                        break;
                    case 500:
                        Message.warning('服务器错误(500)')
                        break;
                    case 501:
                        Message.warning('服务未实现(501)')
                        break;
                    case 502:
                        Message.warning('网络错误(502)')
                        break;
                    case 503:
                        Message.warning('服务不可用(503)')
                        break;
                    case 504:
                        Message.warning('网络超时(504)')
                        break;
                    case 505:
                        Message.warning('HTTP版本不受支持(505)')
                        break;
                    default:
                        Message.warning('连接出错');
                }
            } else {
                Message.warning('连接服务器失败!')
            }
            return Promise.reject(error)
        }
    )
    configMap[key] = {
        get(url, param = {}, headers = {}) {
            return service.get(url, {
                params: param,
                headers,
            })
        },
        post(url, param = null, headers = {}) {
            param = qs.stringify(param, { arrayFormat: "indices", allowDots: true })
            return service.post(url, param, {
                headers,
            })
        },
        jsonPost(url, param = null, headers = {}) {
            return service.post(url, param,  {
                headers: Object.assign({
                    'Content-Type': 'application/json'
                }, headers)
            })
        },
        pdf(url, param = null, headers = {}) {
            return service.post(url, param, { responseType: 'blob' }, {
                headers,
            })
        },
        put(url, param = null, headers = {}) {
            return service.put(url, param, {
                headers,
            })
        },
        file(url, param = null, headers = {}) {
            return service.post(url, param, {
                headers: Object.assign({
                    'Content-Type': 'multipart/form-data'
                }, headers)
            })
        },
        delete(url, param = null, headers = {}) {
            return service.delete(url, {
                param,
                headers: Object.assign({
                    'Content-Type': 'multipart/form-data'
                }, headers)
            })
        }
    }

}

// 定义对外Get、Post、File请求
export default configMap



